package com.fhz.a2102shopjava.ui.imagepicker;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;

import com.fhz.a2102shopjava.R;
import com.fhz.a2102shopjava.bus.MessageEvent;
import com.fhz.a2102shopjava.bus.MessageType;
import com.github.gzuliyujiang.imagepicker.ActivityBuilder;
import com.github.gzuliyujiang.imagepicker.CropImageView;
import com.github.gzuliyujiang.imagepicker.ImagePicker;
import com.github.gzuliyujiang.imagepicker.PickCallback;
import com.google.android.material.button.MaterialButton;

import org.greenrobot.eventbus.EventBus;

/**
 * 时间:2023/8/18
 *
 * @author Mr.Feng
 * 简述: 图库选择
 */
public class ImagePickerActivity extends FragmentActivity {

    private static final String TAG = "ImagePickerActivity";
    private MaterialButton mImagePickerCamera;
    private MaterialButton mImagePickerGallery;
    private final PickCallback cropCallback = new PickCallback() {
        @Override
        public void onPermissionDenied(String[] permissions, String message) {
            Toast.makeText(ImagePickerActivity.this, message, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void cropConfig(ActivityBuilder builder) {
            builder.setMultiTouchEnabled(true)
                    .setGuidelines(CropImageView.Guidelines.ON_TOUCH)
                    .setCropShape(CropImageView.CropShape.OVAL)
                    .setRequestedSize(400, 400)
                    .setFixAspectRatio(true)
                    .setAspectRatio(1, 1);
        }

        @Override
        public void onCropImage(@Nullable Uri imageUri) {
//            Toast.makeText(ImagePickerActivity.this, String.valueOf(imageUri), Toast.LENGTH_SHORT).show();
            Log.d(TAG, "onCropImage: "+String.valueOf(imageUri));
            //把图片地址发送出去
            EventBus.getDefault().post(new MessageEvent.Builder().setMessage(String.valueOf(imageUri)).setType(MessageType.GALLERY).build());
            finish();
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_picker_layout);
        mImagePickerCamera = (MaterialButton) findViewById(R.id.image_picker_camera);
        mImagePickerGallery = (MaterialButton) findViewById(R.id.image_picker_gallery);

        mImagePickerCamera.setOnClickListener(v -> {
            onCamera();
        });

        mImagePickerGallery.setOnClickListener(v ->{
            onGallery();
        });

    }

    public void onCamera() {
        ImagePicker.getInstance().startCamera(this, true, cropCallback);
    }

    public void onGallery() {
        ImagePicker.getInstance().startGallery(this, true, cropCallback);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        ImagePicker.getInstance().onActivityResult(this, requestCode, resultCode, data);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        ImagePicker.getInstance().onRequestPermissionsResult(this, requestCode, permissions, grantResults);
    }

}
